package com.example.iread.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.iread.R;
import com.example.iread.activity.ReaderActivity;
import com.example.iread.bean.ChapterBean;

import java.util.List;

public class BookChapterAdapter extends BaseAdapter {

    Context context;

    List<ChapterBean> list;

    public BookChapterAdapter(Context context, List<ChapterBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    @SuppressLint({"ViewHolder", "InflateParams"})
    public View getView(int i, View view, ViewGroup viewGroup) {
        View item_book_chapter = LayoutInflater.from(context).inflate(R.layout.item_book_chapter, null);
        TextView tv_book_chapter_name = item_book_chapter.findViewById(R.id.tv_book_chapter_name);
//        TextView tv_book_chapter_no = item_book_chapter.findViewById(R.id.tv_book_chapter_no);

        ChapterBean chapterBean = list.get(i);

        tv_book_chapter_name.setText(chapterBean.chapterName);
//        tv_book_chapter_no.setText(String.format("No.%s", i + 1));
        item_book_chapter.setOnClickListener(v -> {
            Intent intent = new Intent(context, ReaderActivity.class);
            intent.putExtra("chapterId", chapterBean.id);
            context.startActivity(intent);
        });
        return item_book_chapter;
    }
}